package com.shanghaionstar.mro.util;

import android.app.Activity;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.Constants;
import com.shanghaionstar.R;
import com.vcyber.sdk.foro.RecDialogForOListener;
import com.vcyber.sdk.foro.TaskControllerForO;
import com.vcyber.voicesdk.text2tts.TtsController;
import com.vcyber.voicesdk.text2tts.TtsListener;

import java.util.Hashtable;

public class VoiceUtil {

    private Activity mrOActivity;
    private RecDialogForOListener rdListener;
    private static VoiceUtil voiceUtil = new VoiceUtil();
    private boolean isInitialized;
    private Hashtable<String, String> mCurrparams;
    String latitude = "";
    String longitude = "";

//    public static VoiceUtil getInstance() {
//        return voiceUtil;
//    }

    public VoiceUtil() {
        //initVoice();
    }

    private String[] array_music;
    private TaskControllerForO tc = null;
    private TtsController ttsController;

    public void updateResource(Activity activity, RecDialogForOListener rdListener) {
        if(this.rdListener == null || this.rdListener != rdListener) {
            this.mrOActivity = activity;
            this.rdListener = rdListener;
        }
//        if (tc != null) {
//            tc.updateResource(activity, rdListener);
//        }
    }

    private void initDefaultData() {
        array_music = mrOActivity.getResources().getStringArray(R.array.array_music);
    }


    public void poi() {
        initVoice();
//        mCurrparams.put("server_url","csajxvdialog.vcyber.com:11006");// f服务器地址
        tc.initializeVD(mrOActivity, mCurrparams, rdListener);
        tc.POISearch();
    }

   public void feedback() {
        initVoice();
        tc.initializeVD(mrOActivity, mCurrparams, rdListener);
        tc.feedback();
    }



    private void initVoice() {
        if (isInitialized) {
            return;
        }
//        initDefaultData();
//        mCurrparams = new Hashtable<String, String>();
////        currparams.put("recordingTimeout", "20");// 网络超时单位s 默认20秒
////        currparams.put("voicek", "1.05");// 音量系数
////        currparams.put("PlayWaittingMusic", "true"); // 是否播放等待音乐
////        currparams.put("MaxVoiceTime", "15000");// 最长录音时间默认15秒
////        currparams.put("SilnceTime", "900"); // 0.9秒静音后自动开始识别
////        currparams.put("PlayError", "false");// 是否播放连接错误音频
////        currparams.put("PlayBeforeWaittingMusicName", array_music[0]); // 前置等待音乐
////        // //8k
////        // 16bit
////        // 单声道
////        currparams.put("HasUI", "false");
////        currparams.put("BeginSilnceTimeOut", "15000"); // 开始静音时等待超时为15秒
////        currparams.put("PlayWaittingMusicName", array_music[1]); // 等待音频播放效果
//
//
//// 地铁二号线淞虹路附近
////        String latitude = "31.2178970";
////        String longitude = "121.3580482";
////        String poi = latitude + longitude;
////        mCurrparams = new Hashtable<String, String>();
////        if (!TextUtils.isEmpty(poi)) {
////            mCurrparams.put("user_id", "user:18512345678&poi:" + poi);// 根据业务需求选择是否传入经纬度
////        } else {
////            mCurrparams.put("user_id", "user:18512345678");
////        }
//        mCurrparams.put("voicek", "1.05");// 音量系数0.9-1.1之间，调整录音识别灵敏度
//        mCurrparams.put("PlayWaittingMusic", "true"); // 是否播放等待音乐
//        mCurrparams.put("MaxVoiceTime", "15000");// 最长录音时间默认15秒
//        mCurrparams.put("SilnceTime", "900"); // 0.9秒静音后自动开始识别
//        mCurrparams.put("PlayError", "false");// 是否播放连接错误音频
//        mCurrparams.put("HasUI", "false");// 是否有UI界面
//
//        mCurrparams.put("PlayBeforeWaittingMusicName", array_music[0]); // 前置等待音乐
//        // //8k
//        // 16bit
//        // 单声道
//        mCurrparams.put("PlayWaittingMusicName", array_music[1]); // 等待音频播放效果
//        tc = TaskControllerForO.getInstance();
        // tc.initializeVD(mrOActivity, mCurrparams, rdListener);

        // 地铁二号线淞虹路附近
//        latitude = "31.2178970";
//        longitude = "121.3580482";
//        String poi = latitude + longitude;
        initDefaultData();
        mCurrparams = new Hashtable<String, String>();
//        if (!TextUtils.isEmpty(poi)) {
//            mCurrparams.put("user_id", "user:18512345678&poi:" + poi);// 根据业务需求选择是否传入经纬度
//        } else {
//            mCurrparams.put("user_id", "user:18512345678");
//        }
        mCurrparams.put("voicek", "1.05");// 音量系数0.9-1.1之间，调整录音识别灵敏度
        mCurrparams.put("PlayWaittingMusic", "true"); // 是否播放等待音乐
        mCurrparams.put("MaxVoiceTime", "15000");// 最长录音时间默认15秒
        mCurrparams.put("SilnceTime", "900"); // 0.9秒静音后自动开始识别
        mCurrparams.put("PlayError", "false");// 是否播放连接错误音频
        mCurrparams.put("HasUI", "false");// 是否有UI界面
        mCurrparams.put("PlayBeforeWaittingMusicName", array_music[0]); // 前置等待音乐
        // //8k
        // 16bit
        // 单声道
        mCurrparams.put("PlayWaittingMusicName", array_music[1]); // 等待音频播放效果

        mCurrparams.put("server_url", Constants.VDFORO_URL_CURRENT);// f服务器地址

//        当前正式域名 ajx2.vdialog.vcyber.com:11006
//        当前测试域名 csajx.vdialog.vcyber.com:11006

        tc = TaskControllerForO.getInstance();
        isInitialized = true;
    }

    public void recycleVD() {
        if (tc != null)
            tc.stopSession();
        mrOActivity = null;
    }

    public void stopSession() {
        if (tc != null) {
            tc.stopSession();
        }
        if (ttsController != null ) {
            ttsController.stopAudio();
            ttsController.setPlaytts(false);
        }
    }

    public void stopTts() {
        if (ttsController != null && ttsController.isPlaytts()) {
            ttsController.stopAudio();
            ttsController.setPlaytts(false);
        }
    }

    public void playFreeText(String freeText) {
        if (ttsController == null) {
            ttsController = new TtsController();
        }
        Hashtable<String, String> currparams = new Hashtable<String, String>();
        currparams.put("time_out", "3000");
        currparams.put("quality", "7");
        currparams.put("audio_format", "1");
        currparams.put("audio_head", "1");
        currparams.put("encoding", "gbk");
        ttsController.setParams(currparams, new TtsListener() {
            @Override
            public void onVoiceResult(byte[] bytes) {
                LogManager.getInstance().i("onVoiceResult");
            }
        }, mrOActivity);
        ttsController.setPlaytts(true);
        ttsController.StartTTS(freeText);
    }
}
